<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/1 0001
 * Time: 下午 2:40
 */
namespace app\api\controller;

use app\common\exception\AuthException;
use app\http\response\ResponseJson;
use think\Controller;
use think\facade\Request;
use think\auth\Auth;

class Base extends Controller{
    use ResponseJson;
    protected $middleware = ['Token']; //中间件检测Token
    private $store_member = '';
    public $start = 0;
    public $length = 20;

    public function initialize(){
        parent::initialize();
//        if(!$this->authLogin()){
//            throw new AuthException([
//               'msg' => '没有登录',
//                'errorCode' => \Yaconf::get('status.login_error')
//            ]);
//        }
        if(!$this->checkAccess(1)){
            throw new AuthException([
                'msg' => '权限不够',
                'errorCode' => \Yaconf::get('status.auth_error')
            ]);
        }
    }

    private function authLogin(){
        $memberInfo = session(\Yaconf::get('app.store_member_sessionkey'));
        if(!empty($memberInfo)){
            $this->store_member = $memberInfo;
            return true;
        }
        return false;
    }

    private function checkAccess($uid){
        $controller = Request::controller();
        $action = Request::action();
        $auth = Auth::instance();
        if($auth->check($controller . '-' . $action,$uid)){
            return true;
        }
        return false;
    }
}